﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Abc.Core;
using Abc.Utils;
using Furion.Schedule;
using Microsoft.Extensions.DependencyInjection;

namespace Apc.Job;

/// <summary>
/// Web初始化延迟执行
/// </summary>
[JobDetail("job_WebInitDelayJob", "Web初始化延迟执行")]
[PeriodSeconds(1, TriggerId = "trigger_WebInitDelayJob", RunOnStart = true)]
public class WebInitDelayJob : IJob
{
    private readonly IServiceScopeFactory _scopeFactory;//高频定时任务
    public WebInitDelayJob(IServiceScopeFactory scopeFactory)
    {
        _scopeFactory = scopeFactory;
    }

    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        try
        {
            if (AppEx.AllowMultithreadingExec("WebInitDelayJob", maxcount: 100) > 1)
            {
                await Task.CompletedTask;
                return;
            }
            using var serviceScope = _scopeFactory.CreateScope();
            //加载或刷新全球化或本地化
            //LEx.LoadOrRefreshLocallization();
            var _cacheService = serviceScope.ServiceProvider.GetService<ISysCacheService>();
            await _cacheService.RefreshCache();
        }
        catch (Exception ex)
        {
            LogEx.Error(ex.ToStringEx(), logFloderName: "启动\\Web初始化延迟执行");
        }
        finally
        {
            AppEx.AllowMultithreadingExecClear("WebInitDelayJob");
        }
    }
}